<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="部门名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        新增
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        prop="name"
        label="部门名称"
        width="260"
      />
      <el-table-column
        prop="code"
        label="部门编码"
        width="120"
      />
      <el-table-column
        prop="leader"
        label="负责人"
        width="120"
      />
      <el-table-column
        prop="phone"
        label="联系电话"
        width="120"
      />
      <el-table-column
        prop="email"
        label="邮箱"
        width="200"
      />
      <el-table-column
        prop="status"
        label="状态"
        width="100"
      >
        <template #default="{row}">
          <el-tag :type="row.status === 1 ? 'success' : 'info'">
            {{ row.status === 1 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        fixed="right"
      >
        <template #default="{row}">
          <el-button
            size="mini"
            type="primary"
            @click="handleUpdate(row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="dialogStatus === 'create' ? '新增部门' : '编辑部门'"
      :visible.sync="dialogVisible"
      width="500px"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="上级部门">
          <el-tree-select
            v-model="temp.parentId"
            :data="deptOptions"
            :props="{label: 'name', value: 'id'}"
            placeholder="请选择上级部门"
            clearable
          />
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="temp.code" />
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="temp.leader" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="temp.phone" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="temp.email" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="temp.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { defineComponent, ref, reactive } from 'vue'
import waves from '@/directive/waves'

export default defineComponent({
  name: 'Department',
  directives: { waves },
  setup() {
    const list = ref([])
    const listLoading = ref(true)
    const listQuery = reactive({
      name: ''
    })

    const dialogVisible = ref(false)
    const dialogStatus = ref('')
    const temp = reactive({
      id: undefined,
      parentId: undefined,
      name: '',
      code: '',
      leader: '',
      phone: '',
      email: '',
      status: 1
    })

    const rules = {
      name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
      code: [{ required: true, message: '请输入部门编码', trigger: 'blur' }]
    }

    const deptOptions = ref([])

    const getList = () => {
      listLoading.value = true
      // TODO: 调用API获取部门列表
      setTimeout(() => {
        list.value = [
          {
            id: 1,
            name: '总公司',
            code: 'HQ',
            leader: '张三',
            phone: '13800138000',
            email: 'zhangsan@example.com',
            status: 1,
            createTime: '2024-01-01 00:00:00',
            children: [
              {
                id: 2,
                name: '技术部',
                code: 'TECH',
                leader: '李四',
                phone: '13800138001',
                email: 'lisi@example.com',
                status: 1,
                createTime: '2024-01-01 00:00:00'
              }
            ]
          }
        ]
        listLoading.value = false
      }, 500)
    }

    const handleFilter = () => {
      getList()
    }

    const resetTemp = () => {
      Object.assign(temp, {
        id: undefined,
        parentId: undefined,
        name: '',
        code: '',
        leader: '',
        phone: '',
        email: '',
        status: 1
      })
    }

    const handleCreate = () => {
      resetTemp()
      dialogStatus.value = 'create'
      dialogVisible.value = true
    }

    const handleUpdate = (row) => {
      Object.assign(temp, row)
      dialogStatus.value = 'update'
      dialogVisible.value = true
    }

    const handleDelete = (row) => {
      // TODO: 调用API删除部门
    }

    const createData = () => {
      // TODO: 调用API创建部门
    }

    const updateData = () => {
      // TODO: 调用API更新部门
    }

    return {
      list,
      listLoading,
      listQuery,
      dialogVisible,
      dialogStatus,
      temp,
      rules,
      deptOptions,
      handleFilter,
      handleCreate,
      handleUpdate,
      handleDelete,
      createData,
      updateData
    }
  }
})
</script>